package com.ruoyi.wms.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.wms.domain.WmsInventory;
import com.ruoyi.wms.service.IWmsInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.wms.domain.WmsTransfer;
import com.ruoyi.wms.service.IWmsTransferService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 调拨单Controller
 * 
 * @author ruoyi
 * @date 2022-03-28
 */
@RestController
@RequestMapping("/transfer")
public class WmsTransferController extends BaseController
{
    @Autowired
    private IWmsTransferService wmsTransferService;

    @Autowired
    private IWmsInventoryService wmsInventoryService;

    /**
     * 查询调拨单列表
     */
    @PreAuthorize(hasPermi = "transfer:transfer:list")
    @GetMapping("/list")
    public TableDataInfo list(WmsTransfer wmsTransfer)
    {
        startPage();
        List<WmsTransfer> list = wmsTransferService.selectWmsTransferList(wmsTransfer);
        return getDataTable(list);
    }

    /**
     * 导出调拨单列表
     */
    @PreAuthorize(hasPermi = "transfer:transfer:export")
    @Log(title = "调拨单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsTransfer wmsTransfer) throws IOException
    {
        List<WmsTransfer> list = wmsTransferService.selectWmsTransferList(wmsTransfer);
        ExcelUtil<WmsTransfer> util = new ExcelUtil<WmsTransfer>(WmsTransfer.class);
        util.exportExcel(response, list, "调拨单数据");
    }

    /**
     * 获取调拨单详细信息
     */
    @PreAuthorize(hasPermi = "transfer:transfer:query")
    @GetMapping(value = "/{tranUuid}")
    public AjaxResult getInfo(@PathVariable("tranUuid") String tranUuid)
    {
        return AjaxResult.success(wmsTransferService.selectWmsTransferByTranUuid(tranUuid));
    }

    /**
     * 新增调拨单
     */
    @PreAuthorize(hasPermi = "transfer:transfer:add")
    @Log(title = "调拨单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsTransfer wmsTransfer)
    {
        return toAjax(wmsTransferService.insertWmsTransfer(wmsTransfer));
    }

    /**
     * 修改调拨单
     */
    @PreAuthorize(hasPermi = "transfer:transfer:edit")
    @Log(title = "调拨单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsTransfer wmsTransfer)
    {
        return toAjax(wmsTransferService.updateWmsTransfer(wmsTransfer));
    }

    /**
     * 删除调拨单
     */
    @PreAuthorize(hasPermi = "transfer:transfer:remove")
    @Log(title = "调拨单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{tranUuids}")
    public AjaxResult remove(@PathVariable String[] tranUuids)
    {
        return toAjax(wmsTransferService.deleteWmsTransferByTranUuids(tranUuids));
    }

    /**
     * 更新库存
     */
    @Transactional
    @PreAuthorize(hasPermi = "transfer:transfer:remove")
    @Log(title = "调拨单(更新库存)", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/upd-inventory/{tranUuid}")
    public AjaxResult updInventory(@PathVariable("tranUuid") String tranUuid)
    {
        // 取数据
        WmsTransfer t = wmsTransferService.selectWmsTransferByTranUuid(tranUuid);

        // 处理记录数（行数）
        AtomicInteger result = new AtomicInteger();

        // 遍历明细
        t.getWmsTransferDetailList().forEach(d -> {

            // 减库存
            WmsInventory i = new WmsInventory();
            i.setUpdateAt(DateUtils.getNowDate());
            i.setFkStockUuid(t.getDictWarehouseFrom());
            i.setFkItemUuid(d.getFkItemUuid());
            i.setFkStockPosUuid(d.getFkStkPosUuidFrom());
            i.setInventoryQty(d.getTransQty().negate()); // 取负数
            wmsInventoryService.insertWmsInventory(i);

            // 加库存
            i.setFkStockUuid(t.getDictWarehouseTo());
            i.setFkStockPosUuid(d.getFkStkPosUuidTo());
            i.setInventoryQty(d.getTransQty());
            wmsInventoryService.insertWmsInventory(i);

            // 记录处理行数
            result.getAndIncrement();

        });

        // 返回处理的记录数
        return toAjax(result.get());
    }


}
